﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DoAn.BUS;
using DoAn.DTO;

namespace DoAn.BAN
{
    public partial class BanTrang1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDanhMuc();
            }
        }
        public void LoadDanhMuc()
        {
            DataTable dt = new DataTable();
            dt = DanhMucBUS.loadDanhMuc();
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count-1; i++)
                {
                    DanhMucDTO dm=new DanhMucDTO();
                    dm.TenDanhMuc=dt.Rows[i]["TenDanhMuc"].ToString();
                    dm.MaDanhMucCha=int.Parse(dt.Rows[i]["MaDanhMucCha"].ToString());
                    dm.MaDanhMuc= int.Parse(dt.Rows[i]["MaDanhMuc"].ToString());
                    TreeNode pNode=new TreeNode(dm.TenDanhMuc,dm.MaDanhMuc.ToString());

                    if (dm.MaDanhMucCha == 0)
                    {
                        trvDanhMuc.Nodes.Add(pNode.Text);
                        for (int j = 0; j < dt.Rows.Count - 1; j++)
                        {
                            DanhMucDTO dm2 = new DanhMucDTO();
                            dm2.TenDanhMuc = dt.Rows[j]["TenDanhMuc"].ToString();
                            dm2.MaDanhMucCha = int.Parse(dt.Rows[j]["MaDanhMucCha"].ToString());
                            if (dm.MaDanhMuc == dm2.MaDanhMucCha)
                            {                                
                                TreeNode pNode2 = new TreeNode(dm2.TenDanhMuc, dm2.MaDanhMucCha.ToString());
                                trvDanhMuc.Nodes[i].Nodes.Add(pNode2.Text);
                            }
                        }
                    }
                }
            }
            
        }
        protected void trvDanhMuc_NodeClick(object source, DevExpress.Web.ASPxTreeView.TreeViewNodeEventArgs e)
        {
            lbMucChon.Text = "Mục Đã Chọn: ";         
            SanDauGiaDTO san = new SanDauGiaDTO();
            DanhMucDTO dm = new DanhMucDTO();
            if (e.Node.Parent.Text != "Node")
            {                
                dm.TenDanhMuc = e.Node.Parent.Text;
                dm.MaDanhMucCha = DanhMucBUS.layMaDanhMucCha(dm);
                dm.TenDanhMuc = e.Node.Text;
                san.MaDanhMuc = DanhMucBUS.layMaDanhMucCon(dm);
            } 
            Session["SanPham"]=san;
            lbMucChon.Text += e.Node.Text;            
        }
    }
}